package yxy.game.pm2.active.command.sdk.weixin.invitefriends;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actyaoqingjiangli;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.sdk.weixin.ActiveModule_邀请好友;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.SdkActiveMessage;
import yxy.game.pm2.message.SdkActiveMessage.WeiXinInviteFriendsInfoRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

/**
 * 邀请好友
 * @author Dell
 *
 */
@MessageType(proto = Command.WeiXinInviteFriendsInfo, classtype = SdkActiveMessage.WeiXinInviteFriendsInfoRequest.class)
public class CMD_Active_邀请好友_Info extends Command<WeiXinInviteFriendsInfoRequest> {
	
	private static final Logger logger = Logger.create(CMD_Active_邀请好友_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, WeiXinInviteFriendsInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();
		SdkActiveMessage.WeiXinInviteFriendsInfoResponse.Builder response = SdkActiveMessage.WeiXinInviteFriendsInfoResponse.newBuilder();
		
		ActiveModule_邀请好友 am = ActiveModule_邀请好友.get(pcode);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
				
		response.setCollect(am.isCollect());
		response.setInvitenum(am.getBeinviteds().size());

		Map<String, actyaoqingjiangli> datamap = ActDataCenter.getAllData(actyaoqingjiangli.class, pcode);
		for(actyaoqingjiangli data : datamap.values()) {
			SdkActiveMessage.InviteFriendsNum.Builder build = SdkActiveMessage.InviteFriendsNum.newBuilder();
			build.setId(data.getId());
			build.setCondition(data.getCondition());
			
			Collection<Currency> curs = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			
//			int canget = am.getDrawids().contains(data.getId()) ? ActiveModule_邀请好友.hadget : 
//				am.getBeinviteds().size() >= data.getCondition() ? ActiveModule_邀请好友.canget : ActiveModule_邀请好友.cannotget;
			int canget = am.getDrawids().contains(data.getId()) ? DrawStatus.hadget.getStatus() :
				am.getBeinviteds().size() >= data.getCondition() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);	
			response.addIfns(build.build());
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, SdkActiveMessage.WeiXinInviteFriendsInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
